package com.sale.succ.components;

import java.util.Date;

import com.sale.succ.utils.DateFormatter;

import eve.ui.CellPanel;
import eve.ui.ControlConstants;
import eve.ui.Input;
import eve.ui.Label;

public class DateSelectPanel extends CellPanel {
	private Input day;
	private Input month;
	private Input year;

	public DateSelectPanel(String name) {
		super();
		this.name = name;
		addNext(getDay()).setCell(VSHRINK).setControl(LEFT);
		addNext(new Label("/")).setCell(VSHRINK).setControl(LEFT);
		addNext(getMonth()).setCell(VSHRINK).setControl(LEFT);
		addNext(new Label("/")).setCell(VSHRINK).setControl(LEFT);
		addLast(getYear()).setCell(VSHRINK).setControl(LEFT);
		initialize();
	}

	//private String name;

	private void initialize() {
		setDate(new Date());
	}

	private Input getDay() {
		if (day == null) {
			day = new Input();
			day.columns = 2;
			day.setPreferredSize(30, 20);
		}
		return day;
	}

	private Input getMonth() {
		if (month == null) {
			month = new Input();
			month.columns = 2;
			month.setPreferredSize(30, 20);

		}
		return month;
	}

	private Input getYear() {
		if (year == null) {
			year = new Input();
			year.columns = 4;
			year.setPreferredSize(40, 20);
		}
		return year;
	}

	private boolean enabled;

	public void setEnabled(boolean enable) {
		getDay().set(ControlConstants.NotEditable, !enable);
		getMonth().set(ControlConstants.NotEditable, !enable);
		getYear().set(ControlConstants.NotEditable, !enable);
		getDay().repaint();
		getMonth().repaint();
		getYear().repaint();
		this.enabled = enable;
	}

	public Date getDate() {
		/*
		String dateStr = getDay().text + "." + getMonth().text + "."
				+ getYear().text;
		try {
			Date date = DateFormatter.stringToDate(dateStr);
		} catch (Exception nfe) {
			new TradeMarketException(nfe.getMessage(), TraderHelper
					.getLabel(TraderConstants.WRONG_DATE)
					+ ":" + name);
		}
		*/
		return DateFormatter.stringToDate(getDay().text + "." + getMonth().text
				+ "." + getYear().text);
	}

	public void setDate(Date date) {
		String dateString = DateFormatter.dateToString(date, "/");
		getDay().text = dateString.substring(0, 2);
		getMonth().text = dateString.substring(3, 5);
		getYear().text = dateString.substring(6, 10);
		if (!enabled) {
			repaint();
		}
	}

	public void setDate(String dateString) {
		getDay().text = dateString.substring(0, 2);
		getMonth().text = dateString.substring(3, 5);
		getYear().text = dateString.substring(6, 10);
		if (!enabled) {
			repaint();
		}
	}
}
